package cn.openea.perpartner.payment.web.points.controller;

import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.PageDomain;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.payment.web.points.dto.PointsDto;
import cn.openea.perpartner.payment.web.points.dto.PointsVo;
import cn.openea.perpartner.payment.web.points.entity.AddPoints;
import cn.openea.perpartner.payment.web.points.entity.Points;
import cn.openea.perpartner.payment.web.points.entity.SubPoints;
import cn.openea.perpartner.payment.web.points.service.IAddPointsService;
import cn.openea.perpartner.payment.web.points.service.IPointsService;
import cn.openea.perpartner.payment.web.points.service.ISubPointsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Weird077
 * @description TODO
 * @date 2022-02-08 22:09
 */
@Api(tags = "积分系统")
@RestController
@RequestMapping("/points")
public class PointsController extends BaseController {
    @Resource
    private IPointsService pointsService;
    @Resource
    private IAddPointsService addPointsService;
    @Resource
    private ISubPointsService subPointsService;

    @ApiOperation("增加积分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "points", value = "积分"),
            @ApiImplicitParam(name = "orderId", value = "订单编号"),
            @ApiImplicitParam(name = "reason", value = "原因"),
            @ApiImplicitParam(name = "sourceId", value = "文档id"),
            @ApiImplicitParam(name = "sceneId", value = "场景id"),
    })
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PointsDto pointsDto) {
        return pointsService.add(pointsDto) ? AjaxResult.success() : AjaxResult.error();
    }

    @ApiOperation("减少积分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "points", value = "积分"),
            @ApiImplicitParam(name = "orderId", value = "订单编号"),
            @ApiImplicitParam(name = "reason", value = "原因"),
            @ApiImplicitParam(name = "sourceId", value = "文档id"),
            @ApiImplicitParam(name = "sceneId", value = "场景id"),
    })
    @PostMapping("/sub")
    public AjaxResult sub(@RequestBody PointsDto pointsDto) {
        return pointsService.sub(pointsDto) ? AjaxResult.success() : AjaxResult.error("操作失败，请检查可用积分是否充足！");
    }

    @ApiOperation("用户积分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
    })
    @PostMapping("/getPoints")
    public AjaxResult selectUserPoints(@RequestBody PointsDto pointsDto) {
        PointsVo pointsVo = pointsService.getUserPoints(pointsDto);
        return AjaxResult.success(pointsVo);
    }
    @ApiOperation("累计积分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
    })
    @PostMapping("/getAccruedPoints")
    public AjaxResult selectCumulativePoints(@RequestBody PointsDto pointsDto) {
        return AjaxResult.success(pointsService.getCumulativePoints(pointsDto));
    }
    @ApiOperation("收入明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "pageNum", value = "当前页数"),
            @ApiImplicitParam(name = "pageSize", value = "当前页显示数"),
            @ApiImplicitParam(name = "sceneId", value = "场景id")
    })
    @PostMapping("/revenue/list")
    public TableDataInfo selectRevenueList(@RequestBody PointsDto dto) {
        startPage();
        List<AddPoints> list = addPointsService.selectList(dto);
        return getDataTable(list);
    }

    @ApiOperation("支出明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "pageNum", value = "当前页数"),
            @ApiImplicitParam(name = "pageSize", value = "当前页显示数"),
            @ApiImplicitParam(name = "sceneId", value = "场景id")
    })
    @PostMapping("/outlay/list")
    public TableDataInfo selectOutlayList(@RequestBody PointsDto dto) {
        startPage();
        List<SubPoints> list = subPointsService.selectList(dto);
        return getDataTable(list);
    }

    @ApiOperation("验证积分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "points", value = "积分"),
    })
    @PostMapping("/validate")
    public AjaxResult checkPoints(@RequestBody PointsDto pointsDto) {
        return pointsService.checkPoints(pointsDto) ? AjaxResult.success("积分充足!") : AjaxResult.error("可用积分不足!");
    }

    @ApiOperation(value = "积分列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数"),
            @ApiImplicitParam(name = "pageSize", value = "当前页显示数")
    })
    @PostMapping("/list")
    public TableDataInfo selectAll(@RequestBody PageDomain pageDomain) {
        startPage();
        List<Points> list = pointsService.list();
        return getDataTable(list);
    }




}
